﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace WindowsFormsAppPieChart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //chart1.Series[0].Points.AddXY("王浩", 10);
            //chart1.Series[0].Points.AddXY(2, 20);
            //chart1.Series[0].Points.AddXY(3, 30);
            //chart1.Text = "XXXX";
            //chart1.Series[0].ChartType = SeriesChartType.Pie;
            ////chart1.Series[0]["PieLabelStyle"] = "Outside";//将文字移到外侧
            //chart1.Series[0]["PieLineColor"] = "Black";//绘制黑色的连线。
            ////chart1.Series.Add(new Series());
            ////chart1.Series[1].Points.AddXY(1, 10);
            ////chart1.Series[1].Points.AddXY("天", 10);
            ////chart1.Series[1].ChartType = SeriesChartType.Pie;

            chart1.Series.Clear();  //清除默认的Series
            //Series Strength = new Series("力量");  //new 一个叫做【Strength】的系列
            //Strength.ChartType = SeriesChartType.Pie;  //设置chart的类型 pie饼状图
            //                                           //Strength.IsValueShownAsLabel = true; //把值当做标签展示（默认false）
            //                                           //chart1.Series["series1"].LegendText = "#VALX";
            //Strength.Label = "#VALX:#PERCENT{P0}";//#VALX显示X轴信息，#PERCENT{P0}以百分比显示Y轴信息，{P0}保留0位小数点
            //int[] arr = new int[] { 20, 20, 20, 40 };
            ////给系列上的点进行赋值，分别对应横坐标和纵坐标的值
            //for (int i = 1; i <= arr.Length; i++)
            //{
            //    Strength.Points.AddXY(i, arr[i - 1]);
            //}
            //Strength.Points[0].Color = Color.Yellow; //实现控制饼状图的颜色,改变对应下标的扇形的颜色
            //                                         //把series添加到chart上
            //chart1.Series.Add(Strength);

            AddSeries("小偷");
            AddSeries("123");
        }

        void AddSeries(string name)
        {

            Series Strength = new Series(name);  //new 一个叫做【Strength】的系列
            Strength.ChartType = SeriesChartType.Pie;  //设置chart的类型 pie饼状图
            //Strength.IsValueShownAsLabel = true; //把值当做标签展示（默认false）
            //chart1.Series["series1"].LegendText = "#VALX";

            //Strength.Label = "#VALX:#PERCENT{P0}";//#VALX显示X轴信息，#PERCENT{P0}以百分比显示Y轴信息，{P0}保留0位小数点

            Strength.Label = "#VALX:#VALY";

            int[] arr = new int[] { 20, 20, 20, 40 };
            //给系列上的点进行赋值，分别对应横坐标和纵坐标的值
            for (int i = 1; i <= arr.Length; i++)
            {
                Strength.Points.AddXY(i, arr[i - 1]);
            }
           // Strength.Points[0].Color = Color.Gold; //实现控制饼状图的颜色,改变对应下标的扇形的颜色
                                                    //把series添加到chart上
            chart1.Series.Add(Strength);

            chart1.Titles.Add(name);
        }


        private void ViewChart(DataTable _dt, string _title)
        {
            this.ct_coll.Series.Clear();
            this.ct_coll.Legends.Clear();

            this.ct_coll.Series.Add(new Series("Data"));
            this.ct_coll.Legends.Add(new Legend("Stores")); //右边标签列

            this.ct_coll.Series["Data"].ChartType = SeriesChartType.Pie;
            this.ct_coll.Series["Data"]["PieLabelStyle"] = "Inside";//Inside 数值显示在圆饼内 Outside 数值显示在圆饼外 Disabled 不显示数值

            this.ct_coll.Series["Data"]["PieLineColor"] = "Black";


            //this.ct_coll.Series["Data"].IsValueShownAsLabel = true;
            //this.ct_coll.Series["Data"].IsVisibleInLegend = true;
            //this.ct_coll.Series["Data"].ShadowOffset = 1;//阴影偏移量

            this.ct_coll.Series["Data"].ToolTip = "#VAL{D} 人";//鼠标移动到上面显示的文字
            this.ct_coll.Series["Data"].BackSecondaryColor = Color.DarkCyan;
            this.ct_coll.Series["Data"].BorderColor = Color.DarkOliveGreen;
            this.ct_coll.Series["Data"].LabelBackColor = Color.Transparent;

            foreach (DataRow dr in _dt.Rows)
            {
                int ptIdx = this.ct_coll.Series["Data"].Points.AddY(Convert.ToDouble(dr["columndata"].ToString()));
                DataPoint pt = this.ct_coll.Series["Data"].Points[ptIdx];
                pt.LegendText = dr["columnname"].ToString() + " " + "#PERCENT{P2}" + " [ " + "#VAL{D} 人" + " ]";//右边标签列显示的文字
                pt.Label = dr["columnname"].ToString() + " " + "#PERCENT{P2}" + " [ " + "#VAL{D} 人" + " ]"; //圆饼外显示的信息

                //  pt.LabelToolTip = "#PERCENT{P2}";
                //pt.LabelBorderColor = Color.Red;//文字背景色 
            }

            // this.ct_coll.Series["Data"].Label = "#PERCENT{P2}"; //
            this.ct_coll.Series["Data"].Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
            this.ct_coll.Series["Data"].Legend = "Stores"; //右边标签列显示信息
            this.ct_coll.Legends["Stores"].Alignment = StringAlignment.Center;
            this.ct_coll.Legends["Stores"].HeaderSeparator = System.Windows.Forms.DataVisualization.Charting.LegendSeparatorStyle.ThickLine;


            this.ct_coll.Titles[0].Text = _title;
            this.ct_coll.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
            this.ct_coll.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;

            int int_count = _dt.AsEnumerable().Select(t => t.Field<int>("columndata")).Sum();
            this.ct_coll.Titles[1].Text = "总人数: " + int_count.ToString("0.00") + " 人";

            /*
            　#VALX      显示当前图例的X轴的对应文本(或数据)
　　#VAL, #VALY,  显示当前图例的Y轴的对应文本(或数据)
　　#VALY2, #VALY3, 显示当前图例的辅助Y轴的对应文本(或数据)
　　#SER:      显示当前图例的名称
　　#LABEL       显示当前图例的标签文本
　　#INDEX      显示当前图例的索引
　　#PERCENT       显示当前图例的所占的百分比
　　#TOTAL      总数量
　　#LEGENDTEXT      图例文本
             */
        }

    }
}
